package com.lancoo.ccas52.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lancoo.ccas52.entity.Course;
import com.lancoo.ccas52.entity.Segment;
import com.lancoo.ccas52.entity.TaskInfo;
import com.lancoo.ccas52.pojo.response.score.CourseLink;
import com.lancoo.ccas52.service.CourseService;
import com.lancoo.ccas52.service.ExternalService;
import com.lancoo.ccas52.service.SegmentService;
import com.lancoo.ccas52.service.TaskInfoService;
import com.lancoo.ccas52.util.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ExternalServiceImpl
 * @Description TODO
 * @Author quin
 * @Date 2023/5/19 10:49
 */
@Service
public class ExternalServiceImpl implements ExternalService {

    @Resource
    private TaskInfoService taskInfoService;
    @Resource
    private CourseService courseService;
    @Resource
    private SegmentService segmentService;

    @Override
    public List<CourseLink> getCourseLink(String schoolId,String term) {
        List<CourseLink> courseLinks = new ArrayList<>();
        TaskInfo taskInfo = taskInfoService.getOne(new LambdaQueryWrapper<TaskInfo>()
                .eq(TaskInfo::getSchoolId, schoolId).eq(TaskInfo::getTerm, term));
        if (ObjUtil.isNull(taskInfo)){
            return courseLinks;
        }
        //获取课程
        courseService.lambdaQuery().eq(Course::getTaskId, taskInfo.getTaskId())
                .eq(Course::getIsEnable, 1).list().forEach(course -> {
                    courseLinks.add(new CourseLink().setCourseNo(course.getBaseCourseId()).setCourseSort(0).setSchoolId(schoolId).setTerm(term).setCollegeId(course.getCollegeId()));
                });
        //获取环节
        segmentService.lambdaQuery().eq(Segment::getTaskId, taskInfo.getTaskId())
                .eq(Segment::getIsEnable, 1).list().forEach(segment -> {
                    courseLinks.add(new CourseLink().setCourseNo(segment.getBaseSegmentId()).setCourseSort(1).setSchoolId(schoolId).setTerm(term).setCollegeId(segment.getCollegeId()));
                });
        return courseLinks;
    }
}
